package com.qf.electronic.pojo;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.qf.electronic.excel.LocalDateConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 
 * </p>
 *
 * @author wu
 * @since 2023-05-04
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("user")
public class User extends BaseEntity implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 用户名
     */
    @TableId("username")
    @NotEmpty(message = "用户名不能为空")
    @Length(min = 6, max = 15, message = "用户名长度只能在6到15个字符之间")
    @ExcelProperty(index = 0, value = "用户名")
    private String username;

    /**
     * 密码
     */
    @TableField("password")
    @ExcelIgnore
    private String password;

    /**
     * 姓名
     */
    @TableField("name")
    @NotEmpty(message = "姓名不能为空")
    @ExcelProperty(index = 1, value = "姓名")
    private String name;

    /**
     * 性别： 0-男，1-女， 2-其他
     */
    @TableField("sex")
    @ExcelProperty(index = 2, value = "性别")
    private Integer sex;

    /**
     * 年龄
     */
    @TableField("age")
    @ExcelProperty(index = 3, value = "年龄")
    private Integer age;

    /**
     * 手机号码
     */
    @TableField("phone")
    @ExcelProperty(index = 4, value = "联系电话")
    private String phone;

    /**
     * 电子邮箱
     */
    @TableField("email")
    @ExcelProperty(index = 5, value = "电子邮箱")
    private String email;

    /**
     * 账号状态：0-冻结，1-正常
     */
    @TableField("state")
    @ExcelIgnore
    private int state = 1;

    /**
     * 入职时间
     */
    @TableField("entry_time")
    @ExcelProperty(index = 6, value = "入职时间", converter = LocalDateConverter.class)
    private LocalDate entryTime;

    /**
     * 离职时间
     */
    @TableField("leave_time")
    @ExcelIgnore
    private LocalDate leaveTime;

    /**
     * 注册时间
     */
    @TableField("created_time")
    @ExcelIgnore
    private LocalDateTime createdTime;

    /**
     * 所属角色
     */
    @TableField("role_id")
    @ExcelIgnore
    private Integer roleId;

    @TableField(exist = false)
    @ExcelIgnore
    private List<GrantedAuthority> authorities;

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return state == 1;
    }
}
